//
//  AddressViewController.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/24.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit
import SwiftyJSON
import SVProgressHUD

protocol AddressViewControllerDelegate {
    func giveTheAddressToProcessOrderVC(dataArr:AddressModel)
}

class AddressViewController: UIViewController,UpdataAddressViewControllerDelegate,UITableViewDelegate,UITableViewDataSource {

    var delegate : AddressViewControllerDelegate?
    
    var myTableView = UITableView(frame: CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT*0.9-64), style: .Plain)
    let ADDRESSCELL = "addressTableViewCell"
    
    var dataArr = NSMutableArray()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.creatNavView()
        self.getAddressData()
        self.creatTabel()
        self.creatAddView()
       
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    


}

extension AddressViewController{
    private func creatNavView(){
        self.view.backgroundColor = UIColor.whiteColor()
        self.automaticallyAdjustsScrollViewInsets = false
        
        self.title = "ADDRESS LIST"
        
        let leftBtn = UIButton(type: .Custom)
        leftBtn.setImage(UIImage(named: "nav_back"), forState: .Normal)
        leftBtn.frame = CGRectMake(0, 0, 30, 30)
        leftBtn.addTarget(self, action: #selector(leftBtnClick), forControlEvents: .TouchUpInside)
        let item = UIBarButtonItem(customView: leftBtn)
        self.navigationItem.leftBarButtonItem = item
        
        
        let rightItemCustomeView   = UIButton(type: UIButtonType.Custom)
        rightItemCustomeView.frame = CGRectMake(0, 0, 30, 30)
        rightItemCustomeView.setImage(UIImage(named:"nav_close"), forState: UIControlState.Normal)
        rightItemCustomeView.addTarget(self, action: #selector(rightBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightItemCustomeView);
        
        
    }
    
    func leftBtnClick(){
        self.navigationController?.popViewControllerAnimated(true)
    }
    
    func rightBtnClick(){
        self.navigationController?.popToRootViewControllerAnimated(true)
    }

}

extension AddressViewController{
    private func getAddressData(){
        let parmas = ["main_page":"address_book","zenid":UserSession.shareInstance.cookie!]
        NetworkTool.shareInstance.request(.GET, urlString: APIBASE, parmas: parmas) { (result, error) in
            if result != nil{
//                let json = JSON(result!)
//                print(json)
                
                let arr = (result as! NSDictionary)["data"] as? [[String:NSObject]]
                for dic in arr!{
                    let model = AddressModel(dictionary: dic)
                    self.dataArr.addObject(model)
                }
                self.myTableView.reloadData()
                
            }else{
                SVProgressHUD.showErrorWithStatus("get cart data error:" + String(error))
                print("get cart data error:" + String(error))
            }
        }
        
        
    }
}

extension AddressViewController{
    private func creatTabel(){
        myTableView.delegate = self
        myTableView.dataSource = self
        self.view.addSubview(myTableView)
        let nib = UINib(nibName: "AddressTableViewCell", bundle: nil)
        myTableView.registerNib(nib, forCellReuseIdentifier: ADDRESSCELL)
        
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArr.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(ADDRESSCELL)
        cell?.selectionStyle = .None
        let model = self.dataArr[indexPath.row] as? AddressModel
        (cell as? AddressTableViewCell)?.model = model
        
        
        return cell!
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return SCREEN_HEIGHT*0.2
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.delegate?.giveTheAddressToProcessOrderVC(self.dataArr[indexPath.row] as! AddressModel)
        self.navigationController?.popViewControllerAnimated(true)
    }
    
}

extension AddressViewController{
    private func creatAddView(){
        let addView = UIView()
        addView.frame = CGRectMake(0, SCREEN_HEIGHT*0.9, SCREEN_WIDTH, SCREEN_HEIGHT*0.1)
        addView.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(addView)
        
        let addBtn  = UIButton(type: .Custom)
        addBtn.frame = CGRectMake(SCREEN_WIDTH*0.05, SCREEN_HEIGHT*0.01, SCREEN_WIDTH*0.9, SCREEN_HEIGHT*0.08)
        addBtn.setTitle("Add shipping address", forState: .Normal)
        addBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        addBtn.backgroundColor = UIColor.blackColor()
        addBtn.addTarget(self, action: #selector(addBtnClick), forControlEvents: .TouchUpInside)
        addView.addSubview(addBtn)
        
        
    }
    
    
    func addBtnClick(){
        let updateVC = UpdataAddressViewController()
        updateVC.delegate = self
        
        self.navigationController?.pushViewController(updateVC, animated: true)
        
    }
    
    func giveTheAddressToAddressVC() {
        self.dataArr.removeAllObjects()
        self.getAddressData()
    }
}























